import re
# def convertUnit():
#     with open('src/components/headBar.vue','r+',encoding="utf8") as f:
#         template = f.read()
#         # print(template)
#         def handle(match):
#             print(match.group(),'ppppppppppppppp')
#             # match = re.sub(r'\s','',match.group())
#             data = re.search(r".*(?=px)",match.group()).group()
#             print(data,')))))))))')
#             # data
#             # return data*16/100
#             print('%.2frem' %(float(data)/100))
#             return ' %.2frem' %(float(data)/100)

#         template = re.sub(r"\s\d*px",handle, template)
#         # f.seek(0,0)
#         f.write(template)
#         # with open('src/views/mine/index.vue','w',encoding="utf8") as f1:
#         #     f1.write(template)

def convertUnit(template,unit,tounit,func):
    '''
    template: 要修改的文本
    unit: 要转换单位
    tounit: 转换后单位
    func: lambda 函数
    '''
    pattern = re.compile(r'\s?-?((\d{0,}\.?\d{0,})|(\d{0,}))'+unit)

    def handle(match):
        '''
        匹配处理函数
        '''
        print(match)
        # ss = re.search(r'\s',match.group()).group()

        # 匹配字符串中是否含有数字，含有字母的话是[a-z]
        pattern = re.compile('[0-9]+')
        # findall是返回所有符合匹配的项，返回形式为数组
        Match = pattern.findall(match.group())
        # if后加变量的意思是判断变量是否为空，不为空则为true，反之为false
        if Match:
            data = re.search(r".*(?="+unit+")",match.group()).group()
            return ' '+func(data,tounit)
        else:
            return match.group()

        

    template = re.sub(pattern,handle, template)
    return template

    

if __name__ == "__main__":
    
    # convertUnit()
    # with open('vue/src/components/phoneFootBar.vue','r+',encoding="utf8") as f:

    #     template = f.read()
    #     template = convertUnit(template,'px','vw',lambda a,b: '%.4f%s'% (float(a)/750*100,b) )
    #     f.seek(0)
    #     f.write('')
    #     f.write(template)
    with open('vue/src/components/login/forget.vue','r+',encoding="utf8") as f:

        template = f.read()
        template = convertUnit(template,'px','rem',lambda a,b: '%.4f%s'% (float(a)/100,b) )
        # 移动光标到开始位置
        f.seek(0)

        f.write('')
        f.write(template)


    # print(convertUnit('''
    # rem
    # fons-size: (20.4rem);
    # hegh: -0rem;
    # heihsd: 30.7rem 30px 30px;
    # ''','rem','px',lambda a,b: '%.0f%s'% (float(a)*100,b) ))

    